package com.qf;

import com.alibaba.dubbo.config.annotation.Service;
import com.qf.search.api.ISearchService;
import com.qf.search.api.entity.TProductSearchDto;
import com.qf.search.api.mapper.TProductSearchDtoMapper;
import com.team5101.vo.ResultBean;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Component
public class SearchServiceImpl implements ISearchService {

    @Autowired
    private TProductSearchDtoMapper tProductSearchMapper;

    @Autowired
    private SolrClient solrClient;


    @Override
    public ResultBean searchByKeyword(String keyword) {
         //创建查询对象
        SolrQuery solrQuery = new SolrQuery();
           solrQuery.set("df","t_product_keywords");
           solrQuery.set(keyword);
           solrQuery.setStart(0);
           solrQuery.setRows(10);
          //封装高亮数据
           solrQuery.setHighlight(true);
           solrQuery.addHighlightField("t_product_name");
           solrQuery.setHighlightSimplePre("<span style='color:blue'>");
           solrQuery.setHighlightSimplePost("</span>");
        try {
            QueryResponse query = solrClient.query(solrQuery);
            List<TProductSearchDto> productSearchDtos = new ArrayList<>();
            SolrDocumentList results = query.getResults();
            Map<String, Map<String, List<String>>> highlighting = query.getHighlighting();
            for (SolrDocument result : results) {
                TProductSearchDto tProductSearchDto = new TProductSearchDto();
                String id = (String) result.getFieldValue("id");
                tProductSearchDto.setId(Long.valueOf(id));
                String t_product_pimage = (String) result.getFieldValue("t_product_pimage");
                tProductSearchDto.settProductPimage(t_product_pimage);
                BigDecimal t_product_sale_price = (BigDecimal) result.getFieldValue("t_product_sale_price");
                tProductSearchDto.settProductSalePrice(t_product_sale_price);
                String t_product_pdesc = (String) result.getFieldValue("t_product_pdesc");
                Map<String, List<String>> stringListMap = highlighting.get(id);
                List<String> t_product_name = stringListMap.get("t_product_name");
                System.out.println(t_product_name);
                tProductSearchDto.settProductName(t_product_name.get(0));
                productSearchDtos.add(tProductSearchDto);
            }
            return ResultBean.success(productSearchDtos);

        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResultBean.error(null,"查询错误");
    }

    @Override
    public ResultBean addProduct(Long pid) {
        return null;
    }

    @Override
    public ResultBean selectAll() {
        List<TProductSearchDto> tProductSearchDtos = tProductSearchMapper.selectAll();
        if (tProductSearchDtos!=null&&!tProductSearchDtos.equals("")){
            List<SolrInputDocument> solrInputDocuments = new ArrayList<>();
            for (TProductSearchDto product : tProductSearchDtos) {
                SolrInputDocument document = new SolrInputDocument();
                document.setField("id",product.getId().toString());
                document.setField("t_product_name",product.gettProductName());
                document.setField("t_product_sale_price",product.gettProductSalePrice().floatValue());
                document.setField("t_product_pimage",product.gettProductName());
                document.setField("t_product_pdesc",product.gettProductPdesc());
               solrInputDocuments.add(document);
            }
            try {
                solrClient.add(solrInputDocuments);
                solrClient.commit();
            } catch (SolrServerException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return ResultBean.success(null,"添加成功");
        }

        return ResultBean.error(null,"添加失败");
    }




}
